#!/opt/lampp/bin/perl -w
use strict;
use warnings;
use DBI;

require('function.pl');
my $db_host = 'localhost';
my $db = 'crm';
my $db_user = 'zjxia';
my $db_password = 'bblovebb';
my $db_conn = DBI->connect("DBI:mysql:database=$db;host=$db_host", $db_user, $db_password);
$db_conn->do('set names gb2312');
my %cities = load_map('city_map_58');
my ($info_url, $city_cn, $keyword_cn) = @ARGV;
my $info_content = `curl -s --speed-time 5 --speed-limit 50000 --connect-timeout 60 -m 300 'http://localhost/meta_search/58lib/58_info.php?$info_url'`;
#			print "$info_content\n";
#			print "http://localhost/meta_search/58lib/58_info.php?$info_url\n";
my ($title, $url, $phone_num, $contact_person, $location, $crawl_number) = ('', '', '', '', '', int(rand(10)));
if ($info_content =~ /\[t\] => ([\d\D]*?)\n/) {
	$title = $1;
}
if ($info_content =~ /\[url\] => (.*?)\n/) {
	$url = $1;
}
if ($info_content =~ /\[m\] => (.*?)\n/) {
	$phone_num = $1;
}
if ($info_content =~ /\[r\] => ([\d\D]*?)\n/) {
	$contact_person = $1;
}
if ($info_content =~ /\[a\] => (.*?)\n/) {
	$location = $1;
}
if ($phone_num eq '') {
	$info_content = `curl -s --speed-time 5 --speed-limit 50000 --connect-timeout 60 -m 300 'http://localhost/meta_search/58lib/58v2.php?url=$url'`;
	if ($info_content =~ /<m>(.+)<\/m>/) {
		$phone_num = $1;
	}
}
if ($phone_num eq '') {
	$info_content = `curl -s --speed-time 5 --speed-limit 50000 --connect-timeout 60 -m 300 '$url'`;
	if ($info_content =~ /&v=(\w+)/) {
		my $ocr_url = "http://192.168.100.234:8080/Ocr58Service.aspx?UserName=jiaohou&Password=jiaohou&image=$1";
		my $ocr_content = `curl -s --speed-time 5 --speed-limit 50000 --connect-timeout 60 -m 300 'http://192.168.100.234:8080/Ocr58Service.aspx?UserName=jiaohou&Password=jiaohou&image=$1'`;
		if ($ocr_content =~ /<div>\s*([\d\-]+)\s*<\/div>/) {
			$phone_num = $1;
		}
		else {
			print STDERR "$ocr_url\n";
		}
	}
}
#			print $info_content;
if ($phone_num eq '') {
#				print "no phone num http://tsquda.3322.org/meta_search/58lib/58v2.php?url=$url\n";
				print "no phone num $url\n";
#				print "no phone num http://tsquda.3322.org/meta_search/58lib/58_info.php?$info_url\n";
	exit;
}
if (execute_scalar("select count(*) from snapshot where phone_num = '$phone_num'", $db_conn) > 0) {
	print "duplicate $phone_num\n";
	exit;
}
#			print "insert into snapshot(domain, query, city, title, url, phone_num, contact_person, crawl_number) values('58'
#                        , '".utf8_to_gb2312($keyword_cn)."'
#                        , '".utf8_to_gb2312($city_cn)."'
#                        , '".utf8_to_gb2312($title)."'
#                        , '$url'
#                        , '$phone_num'
#                        , '".utf8_to_gb2312($contact_person)."'
#                        , $crawl_number
#                        )";
$db_conn->do("insert into snapshot(domain, query, city, title, url, phone_num, contact_person, crawl_number) values('58'
, '".utf8_to_gb2312($keyword_cn)."'
, '".utf8_to_gb2312($city_cn)."'
, '".utf8_to_gb2312($title)."'
, '$url'
, '$phone_num'
, '".utf8_to_gb2312($contact_person)."'
, $crawl_number
)");
print "$keyword_cn\t$city_cn\t$title\t$url\t$phone_num\t$contact_person\n";
